﻿using System;
using System.Configuration;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

using System.IO;
namespace WinnerCMS.Common
{
    /// <summary>
    /// 水印加盖引擎
    /// </summary>
    public class DrawingWaterMarkImage
    {
        private int _transparent = 50;

        public DrawingWaterMarkImage()
        {
            OutPut = null;
            Bottom = 31;
            Right = 88;
            WaterMarkPath = null;
            ImagePath = null;
        }

        #region 属性

        /// <summary>
        /// 获取或设置要修改的图像路径
        /// </summary>
        public string ImagePath { get; set; }

        /// <summary>
        /// 获取或设置在画的图片路径(水印图片)
        /// </summary>
        public string WaterMarkPath { get; set; }

        /// <summary>
        /// 获取或设置水印在修改图片中的右边距
        /// </summary>
        public int Right { get; set; }

        /// <summary>
        /// 获取或设置水印在修改图片中距底部的高度
        /// </summary>
        public int Bottom { get; set; }

        /// <summary>
        /// 获取或设置要绘制水印的透明度,注意是原来图片透明度的百分比
        /// </summary>
        public int Transparent
        {
            get { return this._transparent; }
            set
            {
                if (value >= 0 && value <= 100)
                    this._transparent = value;
            }
        }

        /// <summary>
        /// 获取或设置要输出图像的路径
        /// </summary>
        public string OutPut { get; set; }

        #endregion

        #region 绘制水印
        /// <summary>
        /// 开始绘制水印
        /// </summary>
        public void DrawWaterMark()
        {
            Image modifyImage = null;
            Image drawedImage = null;
            Graphics g = null;
            try
            {
                //建立图形对象
                modifyImage = Image.FromFile(ImagePath);
                drawedImage = Image.FromFile(WaterMarkPath);
                g = Graphics.FromImage(modifyImage);
                //获取要绘制图形坐标
                int x = modifyImage.Width - Right;
                int y = modifyImage.Height - Bottom;
                //设置颜色矩阵
                float[][] matrixItems ={
										   new float[] {1, 0, 0, 0, 0},
										   new float[] {0, 1, 0, 0, 0},
										   new float[] {0, 0, 1, 0, 0},
										   new float[] {0, 0, 0, (float)this._transparent/100f, 0},
										   new float[] {0, 0, 0, 0, 1}};

                ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
                using (ImageAttributes imgAttr = new ImageAttributes())
                {
                    imgAttr.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                    //绘制阴影图像
                    g.DrawImage(
                        drawedImage,
                        new Rectangle(x, y, drawedImage.Width, drawedImage.Height),
                        0, 0, drawedImage.Width, drawedImage.Height,
                        GraphicsUnit.Pixel, imgAttr);
                }
                //保存文件
                
                FileInfo file = new FileInfo(ImagePath);
                ImageFormat imageType = ImageFormat.Gif;
                switch (file.Extension.ToLower())
                {
                    case ".jpg":
                        imageType = ImageFormat.Jpeg;
                        break;
                    case ".gif":
                        imageType = ImageFormat.Gif;
                        break;
                    case ".png":
                        imageType = ImageFormat.Png;
                        break;
                    case ".bmp":
                        imageType = ImageFormat.Bmp;
                        break;
                    case ".tif":
                        imageType = ImageFormat.Tiff;
                        break;
                    case ".wmf":
                        imageType = ImageFormat.Wmf;
                        break;
                    case ".ico":
                        imageType = ImageFormat.Icon;
                        break;
                    default:
                        break;
                }
                byte[] imgData;
                using (MemoryStream ms = new MemoryStream())
                {
                    modifyImage.Save(ms, imageType);
                    imgData = ms.ToArray();
                }
                FileStream fs = null;
                try
                {
                    if (string.IsNullOrEmpty(OutPut))
                    {
                        File.Delete(ImagePath);
                        fs = new FileStream(ImagePath, FileMode.Create, FileAccess.Write);
                    }
                    else
                    {
                        fs = new FileStream(OutPut, FileMode.Create, FileAccess.Write);
                    }

                    fs.Write(imgData, 0, imgData.Length);
                    fs.Dispose();
                }
                catch
                {
                    if (fs != null)
                        fs.Dispose();
                }
            }
            finally
            {
                try
                {
                    if (drawedImage != null)
                    {
                        drawedImage.Dispose();
                    }
                    if (modifyImage != null)
                    {
                        modifyImage.Dispose();
                    }
                    if (g != null)
                    {
                        g.Dispose();
                    }
                }
                catch { }
            }
        }
        #endregion



        #region 加盖水印
        /// <summary>
        /// 加盖水印
        /// </summary>
        /// <param name="strImagePath">保存图片硬盘路径</param>
        public void WatherMark(string strImagePath)
        {
            try
            {
                ImagePath = strImagePath; //图片的路径
                WaterMarkPath = HttpServer.MapPath(ConfigurationManager.AppSettings["DrawingWaterPath"]);

                Right = Convert.ToInt16(ConfigurationManager.AppSettings["DWPright"]);          //水印位置
                Bottom = Convert.ToInt16(ConfigurationManager.AppSettings["DWPbottom"]); ;       //水银位置
                Transparent = Convert.ToInt16(ConfigurationManager.AppSettings["DWPtransparent"]); ;       //透明度
                OutPut = ImagePath;//生成的文件名
                DrawWaterMark();
            }
            catch { }
        }
        /// <summary>
        /// 加盖水印
        /// </summary>
        /// <param name="strImagePath"></param>
        /// <param name="strWaterMarkPath"></param>
        public void WatherMark(string strImagePath, string strWaterMarkPath)
        {
            try
            {
                ImagePath = strImagePath; //图片的路径
                WaterMarkPath = strWaterMarkPath;

                Right = Convert.ToInt16(ConfigurationManager.AppSettings["DWPright"]);          //水印位置
                Bottom = Convert.ToInt16(ConfigurationManager.AppSettings["DWPbottom"]); ;       //水银位置
                Transparent = Convert.ToInt16(ConfigurationManager.AppSettings["DWPtransparent"]); ;       //透明度
                OutPut = ImagePath;//生成的文件名
                DrawWaterMark();
            }
            catch { }

        }
        #endregion
    }
}